package com.jiayou.config

import cn.hutool.core.lang.Console
import com.jiayou.socket.GameSocket
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.Scheduled
import java.time.LocalDateTime


/**
@author 李 双
@data   2020-09-06  12:18
@info
 */
@Configuration
open class GameConfig {

    @Value("${'$'}{logable}")
    var logable: Boolean = true

    val log = LoggerFactory.getLogger(GameConfig::class.java)

    //
    @ExperimentalStdlibApi
    @Scheduled(fixedRate = 2000)
    private fun matchGame() {
        try {
            GameSocket.lock.lock()
            GameSocket.matching()
            if (logable) {
                log.info("对战房间")
                Console.log(GameSocket.pk)
                log.info("对战房间")

                log.info("在线用户")
                Console.log(GameSocket.users)
                log.info("在线用户")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            GameSocket.lock.unlock()
        }

    }


    @ExperimentalStdlibApi
    @Scheduled(fixedRate = 2000)
    private fun tryException() {
        try {
            GameSocket.lock.lock()
            GameSocket.tryException()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            GameSocket.lock.unlock()
        }
    }

}